package com.pos.clothes.controller;

import com.github.pagehelper.PageInfo;
import com.pos.clothes.pojo.Clothes;
import com.pos.clothes.pojo.Norms;
import com.pos.clothes.service.ClothesService;
import com.pos.clothes.service.NormsService;
import com.pos.clothes.util.JsonData;
import com.pos.clothes.util.ValidFlag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequiresPermissions("3")
@RequestMapping("/Norms")
public class NormsController {
    @Autowired
    public NormsService normsService;
    @Autowired
    public ClothesService clothesService;
    @Autowired
    public Norms norms;
    @Autowired
    private Clothes clothes;

    @RequestMapping("/getNorms")
    @ResponseBody
    public PageInfo getNorms(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize) throws Exception{
        PageInfo res = normsService.getNorms(pageNum,pageSize);
        return res;
    }
    @RequestMapping(value = "/searchNorms")
    @ResponseBody
    public Object searchNorms(@RequestParam(value = "flag",required = false) String flag,
                                   @RequestParam(value = "name",required = false) String name,
                                   @RequestParam(value = "idcolor",required = false) Integer idcolor,
                                   @RequestParam(value = "idsize",required = false) Integer idsize,
                                   @RequestParam("pageNum") int pageNum,
                                   @RequestParam("pageSize") int pageSize) throws Exception {
        Norms norms = new Norms();
        Clothes clothes = new Clothes();

        clothes.setName(name);
        clothes.setFlag(flag);

        norms.setClothes(clothes);
        norms.setIdcolor(idcolor);
        norms.setIdsize(idsize);

        PageInfo res = normsService.searchNorms(norms,pageNum,pageSize);
        return res;
    }
    @RequestMapping("deleteNorms")
    @ResponseBody
    public Object deleteNorms(@RequestParam("id") Integer id){
        Integer res = normsService.deleteNorms(id);
        if (res!=0){
            return JsonData.buildSuccess(res,"删除成功");
        }else{
            return JsonData.buildError("删除失败");
        }
    }

    @RequestMapping(value = "/insertNorms")
    @ResponseBody
    public Object insertNorms(@RequestParam("idcategory") Integer idcategory,
                              @RequestParam("name") String name,
                              @RequestParam("idcolor") Integer idcolor,
                              @RequestParam("idsize") Integer idsize,
                              @RequestParam("price") Integer price,
                              @RequestParam("remains") Integer remains,
                              @RequestParam("code") String code){
        //根据参数定义flag
        String flag = ValidFlag.getValidFlag(idcategory);
        //构造clothes
        clothes.setFlag(flag);
        clothes.setName(name);
        clothes.setIdcategory(idcategory);
        //构造norms
        norms.setIdcolor(idcolor);
        norms.setIdsize(idsize);
        norms.setPrice(price);
        norms.setRemains(remains);
        norms.setCode(code);
        //执行插入方法
        Integer res = normsService.insertNorms(norms,clothes);
        if (res ==1){
            return JsonData.buildSuccess(res,"插入成功");
        }else{
            return JsonData.buildError("插入失败");
        }

    }

    @RequestMapping(value = "/updateNorms")
    @ResponseBody
    public Object updateNorms(@RequestParam("idsize") Integer idsize,
                               @RequestParam("idcolor") Integer idcolor,
                               @RequestParam("code") String code,
                               @RequestParam("price") Integer price,
                               @RequestParam("remains") Integer remains,
                               @RequestParam("id")  Integer id,
                              @RequestParam("idclothes") Integer idclothes){
        norms.setIdsize(idsize);
        norms.setIdcolor(idcolor);
        norms.setCode(code);
        norms.setPrice(price);
        norms.setRemains(remains);
        norms.setId(id);
        norms.setIdclothes(idclothes);//测试
        if(normsService.updateNorms(norms) == 1){
            return JsonData.buildSuccess(1,"更新成功");
        }
        return  JsonData.buildError("更新失败");
    }
}
